﻿using System;
using System.Web.UI.WebControls;
using METU_CMT.Business;

namespace METU_CMT
{
    public partial class EditTeamsScreen : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if(!IsPostBack)
            {
                BindTeams();
            }
        }

        protected void OnBtnAddClicked(object sender, EventArgs e)
        {
            Team _team = new Team(txtTeamName.Text);
            ((CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor)).AddTeam(_team);
            BindTeams(  );
        }

        private void BindTeams()
        {
            CourseInstructor _currentCourse = ( CourseInstructor ) EntityFactory.GetCurrentCourse( UserType.Instructor );
            dtgTeams.DataSource = _currentCourse.Teams;
            dtgTeams.DataBind();

            txtTeamName.Focus();
        }

        protected void OnDtgTeamsRowCommand( object sender, GridViewCommandEventArgs e )
        {
            if(e.CommandName.CompareTo( "Delete" ) == 0 )
            {
                int _teamId;
                if(int.TryParse( e.CommandArgument.ToString(  ), out _teamId ) == false)
                {
                    return;
                }

                Team _team = new Team( _teamId );
                CourseInstructor _courseInstructor = ( CourseInstructor ) EntityFactory.GetCurrentCourse( UserType.Instructor );
                _courseInstructor.DeleteTeam( _team );
                BindTeams();
                txtTeamName.Focus();
            }
        }

        protected void OnDtgTeamsRowDeleting( object sender, GridViewDeleteEventArgs e )
        {
            
        }
    }
}